import Vue from 'vue'
import Vuex from 'vuex'
import {config} from '@/common/config.js'
import store from '@/store/index.js'
 
export function wx() {
	if(isTokenExpire()){ 
		uni.login({
			provider: 'weixin',
			success: function(res) {
				handleRequest(res.code,"/app/wx").then(res=>{
					console.log(res)
				}).catch(()=>{
					console.log("登录接口错误")
				})
			}
		});
	}
}

function isTokenExpire() {
	var expire = store.getters.TokenExpire
	if (!expire) return false
	var now = parseInt(Date.parse(new Date()).toString().substr(0, 10))
	if (now > expire) return false
	else return true;
}

async function handleLogin(result) {
	if (result.code === 200) {
		var expire = parseInt(Date.parse(new Date()).toString().substr(0, 10)) + 3600 * 23;
		store.commit('$uStore', {
			name: 'Token',
			value: result.data
		})
		store.commit('$uStore', {
			name: 'TokenExpire',
			value: expire
		})
		Vue.prototype.$isResolve()
	} else if (result.code === 301) { //跳转到注册界面
		uni.redirectTo({
			url: "/pages/login/register"
		})
	} else if (result.code === 400) { //
		
		uni.showToast({
			title: result.message,
			icon: "none",
			mask:true,
		})
	}
}